package com.bosssoft.trainee.rbac.controller;

import com.bosssoft.trainee.rbac.entity.dto.UserDTO;
import com.bosssoft.trainee.rbac.entity.vo.CommonResult;
import com.bosssoft.trainee.rbac.entity.vo.UserVO;
import com.bosssoft.trainee.rbac.entity.vo.UsersVO;
import com.bosssoft.trainee.rbac.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping(value = "/login")
    public CommonResult<UserVO> userLogin(@RequestBody UserVO userVO){
        log.debug(userVO.toString());
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userVO,userDTO);
        UserDTO getUsersDTO =userService.userLogin(userDTO.getUsername(),userDTO.getUserPwd());
            if(getUsersDTO != null){
               BeanUtils.copyProperties(getUsersDTO,userVO);
                return new CommonResult(200,"登陆成功",userVO);
            }
        return new CommonResult(400,"登陆失败",null);
    }

    @PostMapping("/delete")
    public CommonResult<UserVO> userDelete(@RequestBody UserVO userVO){
        UserDTO usersDTO = new UserDTO();
        BeanUtils.copyProperties(userVO,usersDTO);
        int result = userService.userDelete(usersDTO.getUsername());
        if(result>0){
            return new CommonResult(200,"删除成功",result);
        }else {
            return new CommonResult(400,"删除失败",null);
        }

    }

    @PostMapping("/insert")
    public CommonResult<UserVO> userInsert(@RequestBody UsersVO usersVO){
        UserDTO usersDTO = new UserDTO();
        BeanUtils.copyProperties(usersVO,usersDTO);
        int result = userService.userInsert(usersDTO);
        if(result>0){
            return new CommonResult(200,"插入成功",result);
        }else {
            return new CommonResult(400,"插入失败",null);
        }
    }

    @PostMapping("/updatePwd")
    public CommonResult<UserVO> userUpdatePwd(@RequestBody UsersVO usersVO){
        UserDTO usersDTO = new UserDTO();
        BeanUtils.copyProperties(usersVO,usersDTO);
        int result = userService.userUpdatePwd(usersDTO);
        if(result>0){
            return new CommonResult(200,"更改密码成功",result);
        }else {
            return new CommonResult(400,"更改密码失败",null);
        }
    }

    @PostMapping("/updateRole")
    public CommonResult<UserVO> userUpdateRole(@RequestBody UsersVO usersVO){
        UserDTO usersDTO = new UserDTO();
        BeanUtils.copyProperties(usersVO,usersDTO);
        UserDTO adminDTO =  userService.getUserIsAdmin(usersDTO.getUserId());
        if( adminDTO != null ){
            log.info("********是管理员");
            userService.userUpdatePwd(usersDTO);
        }else{
            return new CommonResult(400,"权限不足");
        }
        return new CommonResult(200,"修改权限成功");
    }

    @PostMapping("/getAll")
    public CommonResult<UserVO> getAllUser(){
        List<UserDTO> userDTOList = userService.getAllUser();
        if(userDTOList != null ){
            List<UserVO> userVOList = new ArrayList<>();
            BeanUtils.copyProperties(userDTOList,userVOList);
            log.info(userVOList.toString());
            return new CommonResult(200,"查询成功",userVOList);
        }else{
            return new CommonResult(400,"查询不足");
        }
    }
    @PostMapping("/get")
    public CommonResult<UserVO> getUserById(@RequestBody UserVO userVO){
        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(userVO,userDTO);
        if(userDTO != null ){
            userDTO = userService.getUserById(userDTO.getUserId());
            BeanUtils.copyProperties(userDTO,userVO);
            return new CommonResult(200,"查询成功",userVO);
        }else{
            return new CommonResult(400,"查询不足");
        }
    }
}
